package com.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.constant.SharesConstan;
import com.common.utils.DateUtil;
import com.common.utils.Result;
import com.entity.HintNum;
import com.entity.HintShares;
import com.entity.SharesData;
import com.mapper.HintNumMapper;
import com.service.HintNumService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.service.HintSharesService;
import com.service.SharesDataService;
import com.vo.HintNumVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wty
 * @since 2022-02-11
 */
@Service
public class HintNumServiceImpl extends ServiceImpl<HintNumMapper, HintNum> implements HintNumService {


    @Autowired
    private HintSharesService hintSharesService;

    @Autowired
    private SharesDataService sharesDataService;

    /**
     * 存储提示数据
     */
    @Override
    public void saveNum() {

        Wrapper<HintNum> delwr=new EntityWrapper<>();
        delwr.eq("time",DateUtil.getDate_yyyyMMdd());
        this.delete(delwr);

        Wrapper<HintShares> wr=new EntityWrapper<>();
        wr.eq("time", DateUtil.getDate_yyyyMMdd());
        wr.isNotNull("ts_time");
        wr.orderBy("id");
        List<HintShares> hintSharesList = hintSharesService.selectList(wr);

        Wrapper<SharesData> szwr=new EntityWrapper<>();
        szwr.eq("type", SharesConstan.SZ);
        List<SharesData> sharesData = sharesDataService.selectList(szwr);

        Map<String,SharesData> szMap=new HashMap<>();
        for (SharesData sharesDatum : sharesData) {
            String time = sharesDatum.getTime();
            szMap.put(time,sharesDatum);
        }

        Set<String> setTime=new HashSet<>();
        Map<String,List<HintShares>> tMap=new TreeMap<>();
        for (HintShares hintShares : hintSharesList) {
            Long tsTime = hintShares.getTsTime();
            String time = hintShares.getTime();
            setTime.add(time);

            List<String> times=new ArrayList<>();
            times.add("09:40");
            times.add("09:50");
            times.add("10:00");
            times.add("10:10");
            times.add("10:20");
            times.add("10:30");
            times.add("10:40");
            times.add("10:50");
            times.add("11:00");

            for (String timeVo : times) {
                if(DateUtil.judgeIsSetBiddingTime2("9:20",timeVo,tsTime)){
                    List<HintShares> vo = tMap.get(time+"~"+timeVo);
                    if(!CollectionUtils.isEmpty(vo)){
                        vo.add(hintShares);
                    }else{
                        List<HintShares> to=new ArrayList<>();
                        to.add(hintShares);
                        tMap.put(time+"~"+timeVo,to);
                    }
                }
            }
        }

        List<HintNum> hintNums=new ArrayList<>();
        for (String key : tMap.keySet()) {
            try {
                HintNum hintNum=new HintNum()   ;
                hintNum.setTime(key.split("~")[0]);
                hintNum.setNum(tMap.get(key).size());
                hintNum.setHour(key.split("~")[1]);
            //    hintNum.setZdf( szMap.get(key.split("~")[0]).getZdf());
                hintNum.setZdf("0");
                hintNums.add(hintNum);
            }catch (Exception e){

            }
        }
        if(!CollectionUtils.isEmpty(hintNums)){
            this.insertBatch(hintNums);
        }
    }

    /**
     * 获取提示数据
     * @return
     */
    @Override
    public Result hintNum() {

        Wrapper<HintNum> wr=new EntityWrapper<>();
        List<HintNum> hintNums = this.selectList(wr);

        Map<String, HintNumVo>  voMap=new HashMap<>();
        for (HintNum hintNum : hintNums) {
            String time = hintNum.getTime();
            HintNumVo hintNumVo = voMap.get(time);
            if(null!=hintNumVo){
                doHintNumVo(hintNumVo,hintNum);
            }else{
                HintNumVo vo=new HintNumVo();
                doHintNumVo(vo,hintNum);
                voMap.put(time,vo);
            }
        }
        HintNumVo vo=new HintNumVo();
        List<HintNumVo> hintNumVos=new ArrayList<>();
        if(!CollectionUtils.isEmpty(voMap)){
            for (String key : voMap.keySet()) {
                HintNumVo hintNumVo = voMap.get(key);
                hintNumVos.add(hintNumVo);
                if(key.equals(DateUtil.getDate_yyyyMMdd())){
                    vo=hintNumVo;
                }
            }
        }

//        Integer vot0940 = vo.getT0940();
//        Integer vot0950 = vo.getT0950();
//        Integer vot1000 = vo.getT1000();
//        Integer vot1010 = vo.getT1010();
//        Integer vot1020 = vo.getT1020();
//        Integer vot1030 = vo.getT1030();
//        Integer vot1040 = vo.getT1040();
//        Integer vot1050 = vo.getT1050();
//        Integer vot1100 = vo.getT1100();
//
//        //计算概率
//        for (HintNumVo hintNumVo : hintNumVos) {
//            Integer t0940 = hintNumVo.getT0940();
//            Integer t0950 = hintNumVo.getT0950();
//            Integer t1000 = hintNumVo.getT1000();
//            Integer t1010 = hintNumVo.getT1010();
//            Integer t1020 = hintNumVo.getT1020();
//            Integer t1030 = hintNumVo.getT1030();
//            Integer t1040 = hintNumVo.getT1040();
//            Integer t1050 = hintNumVo.getT1050();
//            Integer t1100 = hintNumVo.getT1100();
//            Double zdf = hintNumVo.getZdf();
//
//            if(t0940!=null&&vot0940!=null){
//                if(t0940>vot0940){
//
//                }
//            }
//        }

        return Result.success(hintNumVos);
    }

    /**
     * 数据处理
     * @param hintNumVo
     * @param hintNum
     */
    private void doHintNumVo(HintNumVo hintNumVo, HintNum hintNum) {
        String hour = hintNum.getHour();
        Integer num = hintNum.getNum();
        hintNumVo.setZdf(new Double(hintNum.getZdf()));
        hintNumVo.setTime(hintNum.getTime());
        if(hour.equals("09:40")){
            hintNumVo.setT0940(num);
        }else if(hour.equals("09:50")){
            hintNumVo.setT0950(num);
        }else if(hour.equals("10:00")){
            hintNumVo.setT1000(num);
        }else if(hour.equals("10:10")){
            hintNumVo.setT1010(num);
        }else if(hour.equals("10:20")){
            hintNumVo.setT1020(num);
        }else if(hour.equals("10:30")){
            hintNumVo.setT1030(num);
        }else if(hour.equals("10:40")){
            hintNumVo.setT1040(num);
        }else if(hour.equals("10:50")){
            hintNumVo.setT1050(num);
        }else if(hour.equals("11:00")){
            hintNumVo.setT1100(num);
        }
    }

}
